import SceneCom from "./SceneCom";
import SceneManager from "./SceneManager";
import GameManager from "../common/GameManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LoadingScene extends SceneCom {

    @property(cc.Node)
    loadingBar: cc.Node

    //加载资源
    loadIndex: number;
    isLoading: boolean;
    isLoadFinish: boolean;
    progress: number;

    comingLoadingCount: number = 0;

    onShow() {
        super.onShow();

        this.loadIndex = 0;
        this.isLoading = false;
        this.isLoadFinish = false;
        this.progress = 0;

        this.loadRes();
    }

    onHide() {
        super.onHide();
    }

    loadRes() {
        var self = this;
        self.isLoading = true;
        // 加载 Texture，不需要后缀名
        cc.loader.loadResDir("sound", (err, res) => {
        });
        cc.loader.loadResDir("prefab", cc.Prefab, (completedCount: number, totalCount: number, item: any) => {
            self.progress = completedCount / totalCount;
        }, function (err) {

        });
    }

    update(dt) {
        if (this.loadingBar) {
            let curProgress: number = this.loadingBar.getComponent(cc.ProgressBar).progress;
            if (this.progress - curProgress >= 0.03) {
                this.loadingBar.getComponent(cc.ProgressBar).progress += 0.01;
            } else {
                this.loadingBar.getComponent(cc.ProgressBar).progress += 0.02;
            }
            if (curProgress >= 1) {
                GameManager.getInstance().sceneManager.switchScene(SceneManager.SceneType.GameScene, SceneManager.SceneSwitchType.UpToDown);
            }
        }
    }

    // update (dt) {}
}
